<?php
namespace app\common\controller;
trait Navs{
    /**
     * 分配菜单到前端模板
     * @param  integer $uid 当前登录会员ID
     * @return void
     */
    function displayNavs($uid){
        $module=request()->module();
        $domain=request()->domain();
        $_navs=config("{$module}_navs.");
        foreach ($_navs as $k => $v) {
            if(isset($v['href'])&&!empty($v['href'])&&false!==strpos($v['href'],'/')&&false===strpos($v['href'],'http')){
                $_navs[$k]['href']=$domain.$v['href'];
            }
            if(isset($v['children'])&&!empty($v['children'])){
                foreach ($v['children'] as $j => $c) {
                    if(isset($c['href'])&&!empty($c['href'])&&false!==strpos($c['href'],'/')&&false===strpos($c['href'],'http')){
                        $_navs[$k]['children'][$j]['href']=$domain.$c['href'];
                    }
                }
            }
        }
        $this->assign('_navs',json_encode($_navs,JSON_UNESCAPED_UNICODE));
        $this->assign('_navs_id',model('navs')->getNavsId());
        if($uid===config('auth.super_id')){
            // 超级管理员
            $list=model('navs')->readAll([],0,['field'=>'id']);
            $ids=[];
            foreach ($list as $v) {
                $ids[]=$v['id'];
            }
            $this->assign('_user_navs_ids',json_encode($ids));
            return;
        }
        $roleIds=model('role')->getRoleIds($uid);
        $ids=model('role')->getNavsIds($roleIds);
        // 由于菜单的特殊性（只需存在子集则必须显示父级，但是后台，父级却不一定选择）
        $pids=model('navs')->readAll(['id'=>['id','in',$ids]],0,['field'=>'pid']);
        $pid=[];
        foreach ($pids as $k => $v) {
            $pid[]=$v['pid'];
        }
        $pid=array_unique($pid);
        $ids=array_merge($ids,$pid);
        $ids=array_unique($ids);

        $this->assign('_user_navs_ids',json_encode(array_values($ids)));
    }
}
